package com.twu.biblioteca;

/**
 * Created by Nishi on 28/12/13.
 *
 * Implementation of Library Management System for Bangalore Public Library
 */

import java.util.ArrayList;
import java.util.Scanner;

public class BibliotecaApp {


    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Menu menu = new Menu();
        Library library = new Library();
        System.out.println(menu.greetingMessage());

        ArrayList<Book> bookList = new ArrayList<Book>();
        ArrayList<Movie> movieList = new ArrayList<Movie>();
        ArrayList<Customer> customers = new ArrayList<Customer>();

        addNewBooks(bookList);
        addNewMovies(movieList);
        addNewCustomers(customers);
        Customer loggedCustomer = new Customer();

        int customerOption;

        do{
            System.out.println(menu.dispalyMainMenu());

            do {
                System.out.println("\nEnter your option <1, 2, 3, 4, 5 or 6 to Exit > : ");
                customerOption = Menu.parseCustomerOption(getInput());
                if(!menu.isValidOption(customerOption)){
                    System.out.println("Incorrect selection !\n");
                    System.out.println(menu.dispalyMainMenu());
                }
            }
            while (!menu.isValidOption(customerOption));

            // List Books
            if (customerOption==1){
                System.out.println("==Available Books==Quantity==");
                System.out.println(library.displayItemDetails(bookList));
            }

            // CheckOut book
            else if (customerOption == 2) {
                    System.out.println("Enter Book Name to checkOut: ");
                    bookTransaction(bookList, customers, 2);
            }
            // Return Book
            else if ((customerOption == 3)) {
                   System.out.println("Enter Book Name to Return: ");
                   bookTransaction(bookList, customers, 3);
            }
            // List Movies
            else if ((customerOption == 4)) {
                    System.out.println("==Available Movies==Year===Director===Available Quantity===rating");
                    System.out.println(library.displayItemDetails(movieList));
                }
                // CheckOut Movie
            else if ((customerOption == 5)) {
                    System.out.println("Enter Movie Name to checkOut: ");
                    String movieName = getInput();
                   library.checkOutItem(movieName, movieList,loggedCustomer);
            }
        }
        while(customerOption != 6);  // while option is not EXIT
    }

    private static void addNewMovies(ArrayList<Movie> movieList) {
        movieList.add(new Movie("Home Alone",1990,"Chris Columbus",2,8));
        movieList.add(new Movie("Toy Story",1994,"John Lasseter",5));
    }

    private static void addNewBooks(ArrayList<Book> list) {
        list.add(new Book("Head First Java",3));
        list.add(new Book("C++ Programming",2));
        list.add(new Book("TDD Approach",1));
    }

    private static void addNewCustomers(ArrayList<Customer> list) {
        list.add(new Customer("111-2222","Nishi","nishi@gmail.com","043435567","sherine"));
        list.add(new Customer("000-1111","Hugo","hugo@gmail.com","043009967","twu"));
    }

    private static Customer getCustomerLoginDetails(){
        System.out.println("Please Login :\nEnter Library Number :  ");
        String library_Number= getInput();
        System.out.println("Enter Password :  ");
        String passWord= getInput();
        return (new Customer(library_Number,passWord));
    }

    private static String getInput() {
        Scanner read = new Scanner(System.in);
        return read.nextLine();
    }
    private static void bookTransaction(ArrayList<Book> bookList, ArrayList<Customer> customers, int option) throws NoSuchFieldException, IllegalAccessException {
        Library library = new Library();
        String bookName = getInput();
        Customer loggedCustomer = getCustomerLoginDetails();
        if (Customer.isValidUser(loggedCustomer, customers)) {
            System.out.println((Customer.getCustomerByNumber(loggedCustomer.getLibrary_number(), customers)).toString());
            Customer loggedUser = Customer.getCustomerByNumber(loggedCustomer.getLibrary_number(),customers);
            if(option == 2){
                library.checkOutItem(bookName, bookList, loggedUser);
            }
            else {
            library.returnItem(bookName, bookList, loggedUser);
            }
        }
        else{
            System.out.println("Login Failed");}
    }

}
